<template>
    <div>
        <el-breadcrumb separator="/" style="margin-bottom: 20px;">
            <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            <el-breadcrumb-item>用户列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <div style="padding-bottom: 20px;">
                <el-input style="width: 30%; margin-right: 20px;" placeholder="请输入内容" v-model="params.query"
                    class="input-with-select">
                    <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
                </el-input>
                <el-button type="primary" @click="$router.push('/useradd')">添加用户</el-button>
            </div>
            <el-table :data="userlist" style="width: 100%" border>
                <el-table-column label="#" width="50" type="index" :index="indexMethod">
                </el-table-column>
                <el-table-column label="姓名">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.username }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="邮箱">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.email }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="电话">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.mobile }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="角色">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.role_name }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="状态">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">
                            <el-switch v-model="scope.row.mg_state" active-color="#409eff" inactive-color="#dcdfe6"
                                @change="zt(scope.row)">
                            </el-switch>
                        </span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="170">
                    <template slot-scope="scope" v-if="scope.row.username !== 'admin'">
                        <el-button size="mini" type="primary" icon="el-icon-edit" @click="Edit(scope.row.id)">
                        </el-button>
                        <el-popconfirm title="确认删除吗？" @confirm="del(scope.row.id)">
                            <el-button type="danger" icon="el-icon-delete" size="mini" slot="reference"></el-button>
                        </el-popconfirm>
                        <el-button size="mini" type="warning" icon="el-icon-setting" @click="GetRole(scope.row)">
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div style="padding-top: 15px;">
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="params.pagenum" :page-sizes="[3, 5, 7, 9]" :page-size="params.pagesize"
                    layout="total, sizes, prev, pager, next, jumper" :total="total">
                </el-pagination>
            </div>
        </el-card>
        <!-- 编辑的dialog -->
        <el-dialog title="修改用户" :visible.sync="editDialog">
            <el-form :model="editForm">
                <el-form-item label="用户名" :label-width="Width">
                    <el-input v-model="username" disabled autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" :label-width="Width">
                    <el-input v-model="editForm.email" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="电话" :label-width="Width">
                    <el-input v-model="editForm.mobile" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="editDialog = false">取 消</el-button>
                <el-button type="primary" @click="editIt">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 分配角色的dialog -->
        <el-dialog title="分配角色" :visible.sync="roleDialog">
            <el-form :model="roleForm">
                <el-form-item :label-width="Width">
                    <p>当前用户：{{ roleForm.username }}</p>
                    <p>当前角色：{{ roleForm.oldRole }}</p>
                    <p>分配新角色：
                        <el-select v-model="rid" placeholder="请选择" @change="sel">
                            <el-option v-for="item in roleSelect" :key="item.id" :label="item.roleName" :value="item.id">
                            </el-option>
                        </el-select>
                    </p>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="roleIt">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
export default {
    data() {
        return {
            userlist: [],
            total: 0,
            params: {
                query: '',
                pagenum: 1,
                pagesize: 3
            },
            editDialog: false,
            Width: '60px',
            editForm: {
                id: 0,
                email: '',
                mobile: '',
            },
            username: '',
            roleForm: {
                username: '',
                oldRole: '',
                type: '',
            },
            roleDialog: false,
            roleSelect: [],
            rid: '',
            id: '',
        }
    },
    methods: {
        // 获取当前id的数据
        async Edit(id) {
            this.editDialog = true;
            const { data: { data, meta } } = await this.$http.get(`users/${id}`, this.editForm);
            if (meta.status == 200) {
                this.editForm.id = data.id;
                this.username = data.username;
                this.editForm.email = data.email;
                this.editForm.mobile = data.mobile;
            }
        },
        // 编辑
        async editIt() {
            const { data: { data, meta } } = await this.$http.put(`users/${this.editForm.id}`, this.editForm);
            if (meta.status !== 200) return this.$message.error('修改失败,请稍后尝试');
            this.$message.success('修改成功');
            this.getUserList();
            this.editDialog = false;
        },
        sel() {
            console.log(this.rid);
        },
        // 分配角色
        async roleIt() {
            const { data: { data, meta } } = await this.$http.put(`users/${this.id}/role`, { rid: this.rid });
            if (meta.status !== 200) return this.$message.error('设置角色失败,请重新尝试');
            this.$message.success('设置角色成功');
            this.roleDialog = false;
            this.getUserList();
        },
        // 获取角色列表
        async GetRole(row) {
            this.roleDialog = true;
            console.log(row);
            this.roleForm.username = row.username;
            this.roleForm.oldRole = row.role_name;
            this.id = row.id;
            const { data: { data, meta } } = await this.$http.get('roles');
            this.roleSelect = data;
        },
        // 单删
        async del(id) {
            const { data: { data, meta } } = await this.$http.delete(`users/${id}`);
            if (meta.status !== 200) return this.$message.error('删除失败,请稍后尝试');
            this.$message.success('删除成功');
            this.getUserList();
        },
        // 搜索
        search() {
            this.getUserList();
        },
        // 状态修改
        async zt(row) {
            if (row.username == 'admin') {
                this.$message.warning('admin管理员不允许修改状态');
                return;
            }
            const { data: { data, meta } } = await this.$http.put(`users/${row.id}/state/${type}`);
            if (meta.status == 200) this.$message.success('修改用户状态成功');
        },
        // 获取用户列表
        async getUserList() {
            const { data: { data, meta } } = await this.$http.get('users', { params: this.params });
            if (meta.status !== 200) return this.$message.error('获取用户列表失败，请重试！');
            this.userlist = data.users;
            this.total = data.total;
        },
        // 改变显示数
        handleSizeChange(i) {
            this.params.pagesize = i;
            this.getUserList();
        },
        // 改变页数
        handleCurrentChange(i) {
            this.params.pagenum = i;
            this.getUserList();
        },
        indexMethod(index) {
            return ++index;
        },
        cancel() {
            this.roleDialog = false;
            this.rid = '';
        }

    },
    created() {
        this.getUserList();
    }
}
</script>
<style lang="less" scoped>
.el-button {
    margin: 0 5px 0 0;
}

/deep/.el-form-item__content {
    margin-left: 0 !important;
}
</style>